﻿using Microsoft.Data.SqlClient.DataClassification;
using Microsoft.EntityFrameworkCore.SqlServer.Query.Internal;
using Microsoft.Extensions.Logging;
using RBAC.MaHaiBo.Entity;
using RBAC.MaHaiBo.Repository.Interface;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace RBAC.MaHaiBo.Service
{
    /// <summary>
    /// 角色菜单关联 服务层
    /// </summary>
    public class MenuRoleService : IMenuRoleService
    {
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<MenuRoleService> logger;
        /// <summary>
        /// 角色菜单关联仓储
        /// </summary>
        IMenuRoleRepository mrRep;
        /// <summary>
        /// 角色仓储
        /// </summary>
        IRoleRepository rRep;
        /// <summary>
        /// 菜单仓储
        /// </summary>
        IMenuRepository mRep;
        /// <summary>
        /// 用户仓储
        /// </summary>
        IUserService uRep;
        /// <summary>
        /// 角色菜单关联仓储
        /// </summary>
        IUserRoleRepository urRep;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="rRep">角色仓储</param>
        /// <param name="mRep">菜单仓储</param>
        /// <param name="mrRep">角色菜单关联仓储</param>
        /// <param name="uRep">用户仓储</param>
        /// <param name="urRep">角色菜单关联仓储</param>
        public MenuRoleService(ILogger<MenuRoleService> logger,
             IRoleRepository rRep,
             IMenuRepository mRep,
              IMenuRoleRepository mrRep,
              IUserService uRep,
              IUserRoleRepository urRep)
        {
            this.logger = logger;
            this.mrRep = mrRep;
            this.rRep = rRep;
            this.mRep = mRep;
            this.uRep = uRep;
            this.urRep = urRep;
        }
        /// <summary>
        /// 添加角色菜单关联
        /// </summary>
        /// <param name="Rid">角色</param>
        /// <param name="Mid">菜单编号集合</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddMenuRole(int Rid, List<int> Mid)
        {
            try
            {
                var count = 0;
                var mr = new MenuRole();
                foreach (var item in Mid)
                {
                    var list = await mrRep.GetAllAsync();
                    var qouen=list.FirstOrDefault(p=>p.Rid== Rid&&p.Mid== item&&!p.IsData);
                    if (qouen==null)
                    {
                        mr.MRid = 0;
                        mr.Rid = Rid;
                        mr.Mid = item;
                        count= await mrRep.AddAsync(mr);
                    }
                }
                return count;

            }
            catch (Exception ex)
            {
                logger.LogError("【角色菜单关联服务层】 添加角色菜单关联 出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 树形菜单列表
        /// </summary>
        /// <returns>返回数据集合</returns>
        public async Task<List<Tmeen>> GetMenu(int Pid=0)
        {
            try
            {
                var list = (from mlist in await mRep.GetAllAsync()
                            where mlist.IsDeleted == false && mlist.Pid== Pid
                            select new Tmeen
                            {
                                id = mlist.Mid,
                                label = mlist.MenuName,
                                children = GetMenu(mlist.Mid).Result
                            });
                return list.ToList();
            }
            catch (Exception ex)
            {
                logger.LogError("【角色菜单关联服务层】 树形菜单列表 出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 树形角色列表
        /// </summary>
        /// <returns>返回数据集合</returns>
        public async Task<List<Tmeen>> GetRole()
        {
            try
            {
                var list = (from rlist in await rRep.GetAllAsync()
                            where rlist.deleteFlag == false
                            select new Tmeen
                            {
                                id = rlist.Rid,
                                label = rlist.RName,
                                children = null
                            });
                return list.ToList();
            }
            catch (Exception ex)
            {
                logger.LogError("【角色菜单关联服务层】 树形角色列表 出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 根据角色编号查询角色菜单关联中的菜单
        /// </summary>
        /// <param name="Rid">角色</param>
        /// <returns>返回根据角色编号查询角色菜单关联中的菜单的编号</returns>
       public async Task<List<int>> BackillMenuRole(int Rid)
        {
            try
            {
                var list = (from mr in await mrRep.GetAsync(p => p.Rid == Rid && !p.IsData)
                            select new 
                            {
                                mr.Mid
                            });
                var Midlist= new List<int>();
                foreach (var item in list)
                {
                    Midlist.Add(item.Mid);
                }
                return Midlist;
            }
            catch (Exception ex)
            {
                logger.LogError("【角色菜单关联服务层】 根据角色编号查询角色菜单关联中的菜单 出错" + ex.Message);
                throw;
            }
        }

     
        


    }
}
